﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALWarehouse
    /// </summary>
    public partial class DALWarehouse
    {
        public DALWarehouse()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbWarehouse");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbWarehouse model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbWarehouse(");
            strSql.Append("warehouseName,flag,content,address,phone,state,warehouseMan,creater,createTime,editUserName,editDate,warebh,companyId)");
            strSql.Append(" values (");
            strSql.Append("@warehouseName,@flag,@content,@address,@phone,@state,@warehouseMan,@creater,@createTime,@editUserName,@editDate,@warebh,@companyId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@warehouseName", SqlDbType.VarChar,50),
					new SqlParameter("@flag", SqlDbType.Int,4),
					new SqlParameter("@content", SqlDbType.VarChar,200),
					new SqlParameter("@address", SqlDbType.VarChar,100),
					new SqlParameter("@phone", SqlDbType.VarChar,50),
					new SqlParameter("@state", SqlDbType.VarChar,50),
					new SqlParameter("@warehouseMan", SqlDbType.VarChar,50),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@editUserName", SqlDbType.VarChar,50),
					new SqlParameter("@editDate", SqlDbType.DateTime),
					new SqlParameter("@warebh", SqlDbType.VarChar,50),
					new SqlParameter("@companyId", SqlDbType.Int,4)};
            parameters[0].Value = model.warehouseName;
            parameters[1].Value = model.flag;
            parameters[2].Value = model.content;
            parameters[3].Value = model.address;
            parameters[4].Value = model.phone;
            parameters[5].Value = model.state;
            parameters[6].Value = model.warehouseMan;
            parameters[7].Value = model.creater;
            parameters[8].Value = model.createTime;
            parameters[9].Value = model.editUserName;
            parameters[10].Value = model.editDate;
            parameters[11].Value = model.warebh;
            parameters[12].Value = model.companyId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbWarehouse model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbWarehouse set ");
            strSql.Append("warehouseName=@warehouseName,");
            strSql.Append("flag=@flag,");
            strSql.Append("content=@content,");
            strSql.Append("address=@address,");
            strSql.Append("phone=@phone,");
            strSql.Append("state=@state,");
            strSql.Append("warehouseMan=@warehouseMan,");
            strSql.Append("creater=@creater,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("editUserName=@editUserName,");
            strSql.Append("editDate=@editDate,");
            strSql.Append("warebh=@warebh,");
            strSql.Append("companyId=@companyId");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@warehouseName", SqlDbType.VarChar,50),
					new SqlParameter("@flag", SqlDbType.Int,4),
					new SqlParameter("@content", SqlDbType.VarChar,200),
					new SqlParameter("@address", SqlDbType.VarChar,100),
					new SqlParameter("@phone", SqlDbType.VarChar,50),
					new SqlParameter("@state", SqlDbType.VarChar,50),
					new SqlParameter("@warehouseMan", SqlDbType.VarChar,50),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@editUserName", SqlDbType.VarChar,50),
					new SqlParameter("@editDate", SqlDbType.DateTime),
					new SqlParameter("@warebh", SqlDbType.VarChar,50),
					new SqlParameter("@companyId", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.warehouseName;
            parameters[1].Value = model.flag;
            parameters[2].Value = model.content;
            parameters[3].Value = model.address;
            parameters[4].Value = model.phone;
            parameters[5].Value = model.state;
            parameters[6].Value = model.warehouseMan;
            parameters[7].Value = model.creater;
            parameters[8].Value = model.createTime;
            parameters[9].Value = model.editUserName;
            parameters[10].Value = model.editDate;
            parameters[11].Value = model.warebh;
            parameters[12].Value = model.companyId;
            parameters[13].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbWarehouse ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbWarehouse ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbWarehouse GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,warehouseName,flag,content,address,phone,state,warehouseMan,creater,createTime,editUserName,editDate,warebh,companyId from tbWarehouse ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            Gift.Model.tbWarehouse model = new Gift.Model.tbWarehouse();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["warehouseName"] != null && ds.Tables[0].Rows[0]["warehouseName"].ToString() != "")
                {
                    model.warehouseName = ds.Tables[0].Rows[0]["warehouseName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["flag"] != null && ds.Tables[0].Rows[0]["flag"].ToString() != "")
                {
                    model.flag = int.Parse(ds.Tables[0].Rows[0]["flag"].ToString());
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["address"] != null && ds.Tables[0].Rows[0]["address"].ToString() != "")
                {
                    model.address = ds.Tables[0].Rows[0]["address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["phone"] != null && ds.Tables[0].Rows[0]["phone"].ToString() != "")
                {
                    model.phone = ds.Tables[0].Rows[0]["phone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["state"] != null && ds.Tables[0].Rows[0]["state"].ToString() != "")
                {
                    model.state = ds.Tables[0].Rows[0]["state"].ToString();
                }
                if (ds.Tables[0].Rows[0]["warehouseMan"] != null && ds.Tables[0].Rows[0]["warehouseMan"].ToString() != "")
                {
                    model.warehouseMan = ds.Tables[0].Rows[0]["warehouseMan"].ToString();
                }
                if (ds.Tables[0].Rows[0]["creater"] != null && ds.Tables[0].Rows[0]["creater"].ToString() != "")
                {
                    model.creater = int.Parse(ds.Tables[0].Rows[0]["creater"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["editUserName"] != null && ds.Tables[0].Rows[0]["editUserName"].ToString() != "")
                {
                    model.editUserName = ds.Tables[0].Rows[0]["editUserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["editDate"] != null && ds.Tables[0].Rows[0]["editDate"].ToString() != "")
                {
                    model.editDate = DateTime.Parse(ds.Tables[0].Rows[0]["editDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["warebh"] != null && ds.Tables[0].Rows[0]["warebh"].ToString() != "")
                {
                    model.warebh = ds.Tables[0].Rows[0]["warebh"].ToString();
                }
                if (ds.Tables[0].Rows[0]["companyId"] != null && ds.Tables[0].Rows[0]["companyId"].ToString() != "")
                {
                    model.companyId = int.Parse(ds.Tables[0].Rows[0]["companyId"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,warehouseName,flag,content,address,phone,state,warehouseMan,creater,createTime,editUserName,editDate,warebh,companyId ");
            strSql.Append(" FROM tbWarehouse ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetWarehouseGoodsList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, warehouseId, goodsBatchNo,  warebh,warehouseName,goodsName,goodsCode, goodsCount,remark, companyId, adminName, createTime,costPrice,modelTypeName ");
            strSql.Append(" FROM view_warehouseGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" ORDER BY createTime DESC ");
        
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetWarehouseGoodsOutList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  id, warehouseId, "
                                       + "  (select top 1 warebh from tbWarehouse where id = tbWarehouseGoodsOut.warehouseId) as warebh, "
                                       + "  (select top 1 warehouseName from tbWarehouse where id = tbWarehouseGoodsOut.warehouseId) as warehouseName, "
                                        + " isnull((select top 1 orderNo from tbOrder where id = tbWarehouseGoodsOut.orderId),'') + ',' +  isnull((select top 1 convert(varchar(15),orderId) from tbCardOrder where orderId = tbWarehouseGoodsOut.orderId),'') as orderNo,"
                                        + "  (select top 1 goodsName from tbGoods where id= tbWarehouseGoodsOut.goodsId) as goodsName, goodsCount, remark, companyId, "
                                       + "  (select top 1 AdminName  from tbAdminLogin where creater = tbWarehouseGoodsOut.creater) as adminName,convert(nvarchar(10),createTime,121) as createTime ");
            strSql.Append(" FROM tbWarehouseGoodsOut ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListByGoodsId(string goodsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct goodsBatchNo ");
            strSql.Append(" FROM tbWarehouse ");

            strSql.AppendFormat(" where goodsId = {0} ", goodsId);

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetWarehouseGoodsList(int iGoodsId,int iModelTypeId)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("select w.id, w.warehouseName + '-' + convert(varchar(10), sum(wg.goodsCount)) as warehouseGoodsName from tbWarehouseGoods wg join tbWarehouse w "
                               + " on wg.warehouseId = w.Id "
                                + " group by w.id, w.warehouseName,wg.goodsId,wg.modelTypeId "
                                + " having   wg.goodsId = {0} and wg.modelTypeId = {1}"
                                , iGoodsId
                                , iModelTypeId
                                );
            return DbHelperSQL.Query(sbSql.ToString());
          }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,warehouseName,flag,content,address,phone,state,warehouseMan,creater,createTime,editUserName,editDate,warebh,companyId ");
            strSql.Append(" FROM tbWarehouse ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbWarehouse ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tbWarehouse T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbWarehouse";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

